package utils

import (
	"fmt"
	"time"
)

const timeFormat = "2006-01-02 15:04:05"

//将日期字符串解析为系统time
func ParseStringToTime(str string) (time.Time, error) {
	loc, err := time.LoadLocation("Local")
	if err != nil {
		return time.Now(), err
	}
	t, err2 := time.ParseInLocation(timeFormat, str, loc)
	if err2 != nil {
		return time.Now(), err2
	}

	return t, nil
}

//将指定时间转化为指定格式字符串：yyyy-MM-dd HH:mm:ss
func FormatTimeToString(t time.Time) string {
	return t.Format(timeFormat)
}

//间隔5s执行函数(任务)
func FuncAfterFunc() {
	task_interval := time.Second * 5
	t := time.AfterFunc(task_interval, func() {
		fmt.Println("func AfterFunc is called!")
	})
	for {
		if t.Reset(task_interval) {
			time.Sleep(task_interval)
		}
	}
}

//New.NewTicker实现定时器功能
func Timer() {

}
